package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.androidkun.xtablayout.XTabLayout;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.ui.fragment.HealManRecoFragment;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.support.view.MeasuredNoScrollViewPager;
import com.ruitu.router_module.support.view.MyNestedScrollView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class HealthManDetailActivity extends BaseActivity {
    private MyNestedScrollView sv_content;
    private MeasuredNoScrollViewPager vp_pro;
    private XTabLayout tab, tab_2;
    private RelativeLayout rl_header, rl_pro, rl_tab;// 健康师头部,tab,tab2

    private String id = "";// 传过来的健康师id
    private String name = "";// 传过来的健康师姓名

    public static final String[] HOME_MODELS = {"推荐", "咨询", "服务"};// tab
    private VpAdapter vpAdapter;
    private List<ProHome> proList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_health_man_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sv_content = findViewById(R.id.sv_content);
        vp_pro = findViewById(R.id.vp_pro);
        tab = findViewById(R.id.tab);
        tab_2 = findViewById(R.id.tab_2);
        rl_header = findViewById(R.id.rl_header);
        rl_pro = findViewById(R.id.rl_pro);
        rl_tab = findViewById(R.id.rl_tab);

        vp_pro.setOffscreenPageLimit(99);
        vp_pro.setScanScroll(false);
        for (int i = 0; i < 3; i++) {
            tab.addTab(tab.newTab().setText(HOME_MODELS[i]), i);
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }
        vpAdapter = new VpAdapter(getSupportFragmentManager());

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            id = bundle.getString("id");
            name = bundle.getString("name");
        }

        if (TextUtils.isEmpty(name)) {
            setHeadTitle("个人主页");
        } else {
            setHeadTitle("个人主页-" + name);
        }

        for (int i = 0; i < 8; i++) {
            ProHome pro = new ProHome();
            pro.setId("26");
            pro.setName("商品" + i);
            pro.setPrice("" + i);
            pro.setPic("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1236673655,1611294387&fm=26&gp=0.jpg");
            proList.add(pro);
        }
        vp_pro.setAdapter(vpAdapter);
        tab.setupWithViewPager(vp_pro);
        tab_2.setupWithViewPager(vp_pro);

        setListeners();
    }

    private void setListeners() {
//        setOnClickListener(rl_call, rl_online);
        sv_content.postDelayed(new Runnable() {
            @Override
            public void run() {
                getTabY();
            }
        }, 100);
        sv_content.setScrollViewListener(new MyNestedScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyNestedScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y >= tabY) {
                    rl_tab.setVisibility(View.VISIBLE);
                } else {
                    rl_tab.setVisibility(View.GONE);
                }
            }
        });
    }

    private int tabY = 0;// tab的y坐标

    private void getTabY() {
//        int[] position = new int[2];
//        tab.getLocationInWindow(position);
//        tabY = position[1];
        tabY = rl_header.getMeasuredHeight() + SizeUtils.dp2px(10);
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return HealManRecoFragment.newInstance();
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS[position];
        }
    }
}
